﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Isaac.Joy.StringHelper
{
    public static class StringExtend
    {
        /// <summary>
        /// 过滤js
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string FilterJs(this string str)
        {
            str = Regex.Replace(str, @"(\<|\s+)o([a-z]+\s?=)", "$1$2", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"(script|frame|form|meta|behavior|style)([\s|:|>])+", "$1.$2", RegexOptions.IgnoreCase);
            return str;
        }

        /// <summary>
        /// id转换成string类型id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string ToId(this int id, int length)
        {
            var sid = new StringBuilder();
            var idlength = id.ToString(CultureInfo.InvariantCulture).Length;
            if (idlength < length)
            {
                sid.Append(id.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                sid.Append(id.ToString(CultureInfo.InvariantCulture).Substring(0, length));
            }
            while (sid.Length < length)
            {
                sid.Insert(0, 0);
            }
            return sid.ToString();
        }

        /// <summary>
        /// 移除Html标记
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RemoveHtmlSp(this string content)
        {
            string regexstr = @"<[^>]*>";
            return Regex.Replace(content, regexstr, string.Empty, RegexOptions.IgnoreCase).Replace("&amp;nbsp;", string.Empty).Replace("&nbsp;", string.Empty).Replace("&amp;quot;", string.Empty).Replace("&lt;", string.Empty).Replace("&gt;", string.Empty);

        }
        /// <summary>
        /// 移除html标记全
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string RemoveHtmlQuan(this string html)
        {
            Regex regex1 = new Regex(@"<script[\s\S]+</script *>", RegexOptions.IgnoreCase);
            Regex regex2 = new Regex(@" href *= *[\s\S]*script *:", RegexOptions.IgnoreCase);
            Regex regex3 = new Regex(@" no[\s\S]*=", RegexOptions.IgnoreCase);
            Regex regex4 = new Regex(@"<iframe[\s\S]+</iframe *>", RegexOptions.IgnoreCase);
            Regex regex5 = new Regex(@"<frameset[\s\S]+</frameset *>", RegexOptions.IgnoreCase);
            Regex regex6 = new Regex(@"\<img[^\>]+\>", RegexOptions.IgnoreCase);
            Regex regex7 = new Regex(@"</p>", RegexOptions.IgnoreCase);
            Regex regex8 = new Regex(@"<p>", RegexOptions.IgnoreCase);
            Regex regex9 = new Regex(@"<[^>]*>", RegexOptions.IgnoreCase);
            Regex regex10 = new Regex(@"<span[\s\S]+</span *>", RegexOptions.IgnoreCase);
            html = regex1.Replace(html, "");
            html = regex2.Replace(html, "");
            html = regex3.Replace(html, " _disibledevent=");
            html = regex4.Replace(html, "");
            html = regex5.Replace(html, "");
            html = regex6.Replace(html, "");
            html = regex7.Replace(html, "");
            html = regex8.Replace(html, "");
            html = regex9.Replace(html, "");
            html = regex10.Replace(html, "");
            html = html.Replace(" ", "");
            html = html.Replace("</strong>", "");
            html = html.Replace("<strong>", "");
            return html;
        }


        /// <summary>
        /// object转int
        /// </summary>
        /// <param name="objectValue"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static int ToInt32(this object objectValue, int defValue)
        {
            int result;
            if (!int.TryParse((objectValue ?? "").ToString(), out result))
            {
                return defValue;
            }
            else
            {
                return result;
            }
        }

        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="content">字符串</param>
        /// <param name="length">截取长度</param>
        /// <param name="replaceChar">追加的符号</param>
        /// <returns></returns>
        public static string SubString(string content, int length, string replaceChar)
        {
            if (content.Length <= length)
            {
                return content;
            }
            else
            {
                return content.Substring(0, length) + replaceChar;
            }
        }

        public static IEnumerable<int> SplitToInt(this string str, string[] separator, StringSplitOptions options)
        {
            var strs = str.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            return strs.Select(sitem => sitem.ToInt32(0)).Where(intitem => intitem != 0);
        }
    }
}
